﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace 日志的使用.自定义的内容
{
    public class SelfResourceFilter : IAsyncResourceFilter
    {
        /// <summary>
        /// 在部分管道之前异步调用
        /// 数据模型绑定前后
        /// </summary>
        /// <param name="context"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
        {
            //阻止 QueryStringValueProvider 提供查询字符串数据。而 QueryStringValueProvider 实例是由 QueryStringValueProviderFactory 工厂类负责创建的
            //则需要将QueryStringValueProviderFactory删除
            var qsValueProviders = context.ValueProviderFactories.OfType<QueryStringValueProviderFactory>();
            if (qsValueProviders != null && qsValueProviders.Any())
            {
                context.ValueProviderFactories.RemoveType<QueryStringValueProviderFactory>();
            }
            //context.Result= new JsonResult(""); //Result可以直接作为短路器使用,只要给这个属性赋值, 直接短路

            ResourceExecutedContext contexted= await next.Invoke(); //执行后的上下文

            //contexted.Result = new JsonResult("");


        }
    }
}
